package com.kfm.jdbc.day0405;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class CachedPool {

    public static void main(String[] args) {
        MyThreadFactory myThreadFactory = new MyThreadFactory();
        // 创建线程池
        ExecutorService executorService = Executors.newCachedThreadPool(myThreadFactory);

        // 给线程池中的线程分配任务
//        executorService.execute(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println(Thread.currentThread().getName() + " is running");
//            }
//        });


        for (int i = 0; i < 10; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + " is running");
                }
            });
        }
    }
}


class MyThreadFactory implements ThreadFactory {

    int i;
    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r, "自定义线程" + i ++);
    }
}
